
/* Generated by Interface Builder */

#import "CookieFile.h"
#import <strings.h>
#import <string.h>
#import <c.h>
#import <libc.h>

@implementation CookieFile

+ (CookieFile *)findName :(char *)name fromList:(List *)list
{
	int count,maxcount;

	for(count = 0,maxcount = [list count]; count < maxcount; count++)
	{
		char *myfilename = [[list objectAt:count] filename];

		if(!strcasecmp(name,myfilename))
			return [list objectAt:count];
	}

	return NULL;
}

- (int)filelength
{
	return file_length;
}

- setFilelength :(int)length
{
	file_length = length;

	return self;
}

- fillOut
{
	char mypath[MAXPATHLEN];
	NXStream *stream;

	sprintf(mypath,"%s/Library/Cookies/%s",getenv("HOME"),filename);

	stream = NXMapFile(mypath, NX_READONLY);
	if(!stream)
	{
		sprintf(mypath,"/LocalLibrary/Cookies/%s",filename);
		stream = NXMapFile(mypath,NX_READONLY);
	}

	if(!stream)
		return 0;

	[self readCookies :stream];

	NXCloseMemory(stream, NX_FREEBUFFER);
	return self;
}

- initFromName :(char *)name fromList:(List *)list
{
	if([CookieFile findName :name fromList:list])
		return FALSE;

	[self init];
	strcpy(filename,name);

	return self;
}

- init
{
	[super init];

	filename[0] = 0;
	flags.selected = TRUE;
	cookies = NULL;

	return self;
}

- setSelected :(BOOL)value
{
	flags.selected = value;

	return self;
}

- (BOOL)selected
{
	return flags.selected;
}

- (BOOL)readCookies :(NXStream *)stream
{
	String *tempString;
	BOOL foundCookie = FALSE;
	Cookie *tempCookie;

	tempString = [[[String alloc] init] preAlloc:1024];
	tempCookie = [[Cookie alloc] init];
	if(!cookies)
		cookies = [[List alloc] init];

	while([tempString readString:stream])
	{
		switch([tempString string][0])
		{
			case '#'	:
				if([tempString string][1] == '#')
					break;
				if(foundCookie)
				{
					if([tempCookie blank])
						[tempCookie free];
					else
						[cookies addObject:tempCookie];
					tempCookie = [[Cookie alloc] init];
					foundCookie = FALSE;
				}

				[tempCookie addLine:&([tempString string][1])];
				foundCookie = TRUE;
				break;

			default	:
				if(foundCookie)
				{
					[tempCookie addLine:&([tempString string][1])];
				}
			else
				{
					if([tempString string][0])
					{
						[tempCookie addLine:[tempString string]];
						[cookies addObject:tempCookie];
						tempCookie = [[Cookie alloc] init];
					}
				}
			break;
		}
	}


	if((foundCookie) && ![tempCookie blank])
		[cookies addObject:tempCookie];
	else
		[tempCookie free];

	[tempString free];

	return TRUE;
}

- (int)count
{
	if(!cookies)
		[self fillOut];

	if(cookies)
		return [cookies count];

	return 0;
}

- (Cookie *)pickRandom
{
	if(!cookies)
		[self fillOut];

	return [cookies objectAt:random()%[cookies count]];
}

- (Cookie *)pickNumber :(int)number
{
	if(!cookies)
		[self fillOut];

	return [cookies objectAt:number];
}

- (void)free
{
	if(cookies)
	{
		[cookies freeObjects];
		[cookies free];
	}

	[super free];
}

- (char *)filename
{
	return filename;
}

- setFilename :(char *)string
{
	strcpy(filename,string);

	return self;
}

@end
